package com.zw.pdm.module.designtask.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.designtask.CosDesign;
import com.zw.pdm.entity.designtask.CosWeight;
import com.zw.pdm.entity.designtask.DsnProStruct;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.designtask.service.WeightHoursService;
import com.zw.pdm.module.report.model.Project;
import com.zw.pdm.module.report.service.SheetService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pdm.constant.Constant.GROUP_SPECTYPE;


/**
 * @Description: 设计工时、设备重量 控制层
 * 权限：
 * dsn:proCost:view 信息列表查看
 * dsn:proCost:edit 编辑/保存
 * dsn:proCost:del 删除
 * dsn:proCost:commit 提交
 *
 * dsn:costInforma:view 审核列表查看
 * dsn:costInforma:success 审核明细 审核/驳回
 *
 * dsn:proQuery:view 成本查询
 * @Author: China.wzy
 */
@Controller
@RequestMapping("/designTask/dataEntry")
@SuppressWarnings("all")
public class WeightHoursController extends BaseController {

    @Autowired
    private WeightHoursService weighthoursService;
    @Autowired
    private SheetService sheetService;
    @Autowired
    private DictCommonService dictCommonService;

    /**
     * 设计成本输出页面
     * @param mode
     * @return
     */
    @GetMapping("/costDesign")
    public String WeightHoursCostDesign(Model mode) {
        mode.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "designtask/add/designCost";
    }

    /**
     * 查询项目信息
     * @param q
     * @return
     */
    @GetMapping("/project/list")
    @ResponseBody
    public List<Project> proList(String q) {
        return sheetService.findProjectByCode(q);
    }

    /**
     * 查询设备部分
     * @param code
     * @param type
     * @return
     */
    @GetMapping("/part/list")
    @ResponseBody
    public List<DsnProStruct> partList(String code, Integer type) {
        return weighthoursService.findPartByProjectCode(code, type);
    }

    /**
     * 加载设计工时
     * @param cosDesign
     * @return
     */
    @ResponseBody
    @RequiresPermissions({"dsn:proCost:view"})
    @PostMapping("/design")
    public JsonResult design(CosDesign cosDesign){
        List<CosDesign>list=weighthoursService.designHours(cosDesign);
        return jsonResult(list);
    }

    /**
     * 加载设备重量
     * @param cosWeight
     * @return
     */
    @ResponseBody
    @RequiresPermissions({"dsn:proCost:view"})
    @PostMapping("/weight")
    public JsonResult weight(CosWeight cosWeight){
        List<CosWeight>list=weighthoursService.designWeight(cosWeight);
        return jsonResult(list);
    }

    /**
     * 保存/修改设计工时
     * @param data
     * @return
     */
    @ResponseBody
    @PostMapping("/hoursSave")
    @RequiresPermissions({"dsn:proCost:edit"})
    public JsonResult hoursSaveData(CosDesign data){
//        return jsonResult(weighthoursService.saveData(data));
        JsonResult jsonResult = new JsonResult();
        int flg =weighthoursService.hoursSaveData(data);
        if (flg>0){
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setData(flg);
        }else {
            jsonResult.setType(JsonResult.ResultType.ERROR);
        }
        return jsonResult;
    }

    /**
     * 删除设计工时
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/delHours")
    @RequiresPermissions({"dsn:proCost:del"})
    public JsonResult delHours(Integer id){
        return jsonResult(weighthoursService.delHouseOne(id));
    }

    /**
     * 保存/修改设备重量
     * @param Entry
     * @return
     */
    @ResponseBody
    @PostMapping("/weightSave")
    @RequiresPermissions({"dsn:proCost:edit"})
    public JsonResult weightSaveData(CosWeight Entry){
//        return jsonResult(weighthoursService.saveData(data));
        JsonResult jsonResult = new JsonResult();
        int flg =weighthoursService.weightSaveData(Entry);
        if (flg>0){
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setData(flg);
        }else {
            jsonResult.setType(JsonResult.ResultType.ERROR);
        }
        return jsonResult;
    }

    /**
     * 删除设备重量
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/delWeight")
    @RequiresPermissions({"dsn:proCost:del"})
    public JsonResult delWeight(Integer id){
        return jsonResult(weighthoursService.delWeightOne(id));
    }

    /**
     * 提交数据——更改提交状态
     * @param cosDesign
     * @return
     */
    @ResponseBody
    @PostMapping("/submitData")
    @RequiresPermissions({"dsn:proCost:commit"})
    public JsonResult submitData (CosDesign cosDesign){
        return jsonResult(weighthoursService.submitData(cosDesign));
    }

}
